package cn.jwelll.bxcstock.base;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import cn.jwelll.bxcstock.ui.dialog.DialogUtils;
import cn.jwelll.bxcstock.util.LogUtil;

/**
 * 采用MVP模式的基础Fragment，View层，创建Presenter MVPBaseFragment
 */
public abstract class MVPBaseFragment<V, P extends BasePresenter<V>> extends BaseFragment {
    protected P mPresenter; // Presenter对象

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = createPresenter();
        mPresenter.attachView((V) this);
        mPresenter.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
    }

    protected abstract P createPresenter();
    private Dialog loading;
    private Dialog dialog;

    public void showSucceed() {
        if (loading != null) {
            loading.dismiss();
        }
    }

    public void showFailed(String message) {
        if (loading != null) {
            LogUtil.e("msg", "loading" + loading);
            loading.dismiss();
        }
        if (message.equals("Current user did not login to the application!")) {
            showDialog("登录失效");
        } else {
            showDialog(message);
        }
    }

    public void showDialog(String message) {
        if(message.equals("登录失效")){
            DialogUtils.CallbackListener  listener=(v) -> {
//                    Intent intent=new Intent(getActivity(), LoginActivity.class);
//                    startActivity(intent);
//                    getActivity().finish();
            };
            dialog = new DialogUtils.Builder(getActivity())
                    .setPositiveButton("确定",null,listener)
                    .setMessage(message)
                    .creat();
            dialog.show();
        }else {
            dialog = new DialogUtils.Builder(getActivity())
                    .setMessage(message)
                    .creat();
            dialog.show();
        }
    }

    public void showLoading(String message) {
        loading = new DialogUtils.Builder(getActivity())
                .setType(DialogUtils.LOADING)
                .setOutTouch(false)
                .setMessage(message)
                .creat();
        loading.setCancelable(true);
        loading.show();
    }
}
